package com.dtw.ditouwang.ui.coin

import android.os.Bundle
import android.text.Html
import android.text.TextUtils
import android.view.View
import com.dtw.ditouwang.R
import com.dtw.ditouwang.beans.CommentBean
import com.dtw.ditouwang.beans.CommonBean
import com.dtw.ditouwang.beans.ProductDetailBean
import com.dtw.ditouwang.models.WebApi
import com.dtw.ditouwang.repositories.UserRepository
import com.dtw.ditouwang.ui.BaseActivity
import kotlinx.android.synthetic.main.activity_product_detail.*
import kotlinx.android.synthetic.main.fragment_more.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.text.DecimalFormat

class ProductDetialActivity : BaseActivity(), View.OnClickListener {
    var imagePathList = mutableListOf<String>()
    var imagePageAdapter = ImagePageAdapter(imagePathList)
    var productBean: ProductDetailBean.DataBean? = null


    companion object {
        val KeyProductID = "KeyProductID"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_product_detail)
        viewPager.adapter = imagePageAdapter
        buttonConfirm.setOnClickListener(this)
        webApi.getDetail(UserRepository.getInstance().userID, intent.getIntExtra(KeyProductID, 0)).enqueue(object : Callback<ProductDetailBean> {
            override fun onFailure(call: Call<ProductDetailBean>, t: Throwable) {
                t.printStackTrace()
            }

            override fun onResponse(call: Call<ProductDetailBean>, response: Response<ProductDetailBean>) {
                var productDetailBean = response.body()
                if (productDetailBean != null) {
                    productBean = productDetailBean.data
                    imagePathList.addAll(productDetailBean.data.img)
                    imagePageAdapter.notifyDataSetChanged()
                    textMony.text = Html.fromHtml("<font color='#cc0000'><big><big><big>￥${DecimalFormat("#.#####").format(productDetailBean.data.gold_price)}金币</big></big></big></font>(${DecimalFormat("#.#####").format(productDetailBean.data.price)}元)")
                    textName.text = productDetailBean.data.commodity_name
                    textType.text = Html.fromHtml("<font color='#aaaaaa'>型号 </font>${productDetailBean.data.type}")
                    textPosition.text = Html.fromHtml("<font color='#aaaaaa'>送至 </font>${productDetailBean.data.address}")
                }
            }

        })
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.buttonConfirm -> {
                if(TextUtils.isEmpty(UserRepository.getInstance().userBean.data.address)||
                        TextUtils.isEmpty(UserRepository.getInstance().userBean.data.name)||
                        TextUtils.isEmpty(UserRepository.getInstance().userBean.data.username)){
                    showToast("请到个人资料页面完善用户信息")
                    return
                }
                if (productBean != null) {
                    webApi.commitOrder(UserRepository.getInstance().userID, intent.getIntExtra(KeyProductID, 0), productBean?.address + "asdfasdf", productBean?.commodity_name)
                            .enqueue(object : Callback<CommonBean> {
                                override fun onFailure(call: Call<CommonBean>, t: Throwable) {
                                    t.printStackTrace()
                                }

                                override fun onResponse(call: Call<CommonBean>, response: Response<CommonBean>) {
                                    var orderBean = response.body()
                                    if (orderBean == null) {
                                        showToast("兑换失败")
                                        return
                                    }
                                    if (!orderBean.isResult) {
                                        showToast(orderBean.msg)
                                        return
                                    }
                                    showToast("兑换成功")
                                    finish()
                                }
                            })
                }
            }
        }
    }
}